package com.aidex.system.service.impl;

import com.aidex.common.core.domain.entity.SysPost;
import com.aidex.common.core.service.impl.BasicServiceImpl;
import com.aidex.common.exception.BizException;
import com.aidex.common.exception.CustomException;
import com.aidex.common.utils.NumberUtils;
import com.aidex.system.common.SysErrorCode;
import com.aidex.system.domain.SysUserPost;
import com.aidex.system.mapper.SysPostMapper;
import com.aidex.system.mapper.SysUserPostMapper;
import com.aidex.system.service.SysPostService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

@Transactional(readOnly = true)
@Service
public class SysPostServiceImpl extends BasicServiceImpl<SysPostMapper, SysPost> implements SysPostService {

    @Autowired
    private SysUserPostMapper userPostMapper;

    @Override
    public List<SysPost> selectListByUser(QueryWrapper<SysPost> wrapper) {
        return mapper.selectListByUser(wrapper);
    }

    @Override
    public void checkPostNameUnique(SysPost post) {
        QueryWrapper<SysPost> wrapper = new QueryWrapper();
        wrapper.eq("post_name", post.getPostName());
        wrapper.ne("id", post.getId());
        if (mapper.selectCount(wrapper) > 0) {
            throw new BizException(SysErrorCode.B_SYSPOST_PostNameAlreadyExist);
        }
    }

    @Override
    public void checkPostCodeUnique(SysPost post) {
        QueryWrapper<SysPost> wrapper = new QueryWrapper();
        wrapper.eq("post_code", post.getPostCode());
        wrapper.ne("id", post.getId());
        if (mapper.selectCount(wrapper) > 0) {
            throw new BizException(SysErrorCode.B_SYSPOST_PostCodeAlreadyExist);
        }
    }

    @Override
    @Transactional
    public int deleteById(Serializable id) {
        SysPost entity = selectById(id);
        if(entity != null) {
            QueryWrapper<SysUserPost> wrapper = new QueryWrapper<>();
            wrapper.eq("post_id", id);
            if (userPostMapper.selectCount(wrapper) > 0) {
                throw new CustomException(String.format("%1$s已分配,不能删除", entity.getPostName()));
            }
            return super.deleteById(id);
        }
        return 0;
    }

    @Override
    @Transactional
    public int deleteBatchIds(Collection<? extends Serializable> idList) {
        int flag = 0;
        if (idList != null && !idList.isEmpty()) {
            for (Serializable id : idList) {
                flag += deleteById(id);
            }
        }
        return flag;
    }

    @Override
    @Transactional
    public int insert(SysPost sysPost) {
        checkPostNameUnique(sysPost);
        checkPostCodeUnique(sysPost);
        return super.insert(sysPost);
    }

    @Override
    @Transactional
    public int updateById(SysPost sysPost) {
        checkPostNameUnique(sysPost);
        checkPostCodeUnique(sysPost);
        return super.updateById(sysPost);
    }

    @Override
    public int findMaxSort(){
        return NumberUtils.nextOrder(mapper.findMaxSort());
    }

}
